package com.example.springboot_system.Controller;

import com.example.springboot_system.pojo.Drug;
import com.example.springboot_system.service.DrugsService;
import com.example.springboot_system.utils.ResponseResult;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/drugs")
public class DrugsController {

    @Autowired
    private  DrugsService drugsService;

    //药物管理页面
    @GetMapping("/toDrugsPages")
    public String toDrugsPage(HttpSession session,Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,2);
        //通过model把全部药物数据给页面
        List<Drug> drugslist=drugsService.getAllDrugs(session,model);
        System.out.println("控制层查询全部药物数据："+drugslist);
        PageInfo<Drug> pageInfo = new PageInfo<Drug>(drugslist);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute(drugslist);
        return "drugStore/drugManage";
    }

    //得到模块框需要的药物信息
    @RequestMapping("/seeid")
    @ResponseBody
    public ResponseResult seeId(String id, Model model){
        //得到药物id
        System.out.println(id);
        Drug selectdrug=drugsService.getTheDrugs(id);
        ResponseResult result = new ResponseResult(201,"得到药物信息",selectdrug);
        return result;
}

    //根据表单更新药物表
    @RequestMapping("/updateDrugs")
    @ResponseBody
    public ResponseResult updateDrugs(Drug drug){
        System.out.println("控制层更新药物信息："+drug);
        ResponseResult result=drugsService.updateDrugs(drug);
        return result;
    }

    //删除一条药物信息
    @PostMapping("/deletedrugs")
    @ResponseBody
    public ResponseResult deletedrugs(String id){
        System.out.println("控制层删除药物信息："+id);
        ResponseResult result=drugsService.deleteDrug(id);
        return result;
    }

    //查询全部药物名
    @RequestMapping("/findAllDrugsName")
    @ResponseBody
    public ResponseResult findAllDrugsName(){
        ResponseResult result= drugsService.findAllDrugsName();
        System.out.println(result);
        return  result;
    }

    //查询全部药物效果
    @RequestMapping("/findAllDrugsFunction")
    @ResponseBody
    public ResponseResult findAllDrugsFunction(){
        ResponseResult result= drugsService.findAllDrugsFunction();
        System.out.println(result);
        return  result;
    }


    //查询全部剂型
    @RequestMapping("/findAllDrugsReagent")
    @ResponseBody
    public ResponseResult findAllDrugsReagent(){
        ResponseResult result= drugsService.findAllDrugsReagent();
        System.out.println(result);
        return  result;
    }

    //限制性查找
    @RequestMapping("/selectDrugsLimit")
    @ResponseBody
    public ResponseResult selectDrugsLimit(@Param("selectdrugsname") String selectdrugsname,@Param("selectdrugsReagent") String selectdrugsReagent,@Param("selectLimit") int selectLimit,@Param("selectdrugsFunction") String selectdrugsFunction,HttpSession session){
        System.out.println("控制层查看选择为"+selectdrugsFunction+"-"+selectdrugsname+"-"+selectdrugsReagent+"-"+selectLimit);
        ResponseResult result=drugsService.findTheDrugsBySelect(selectdrugsname,selectdrugsReagent,selectLimit,selectdrugsFunction);
        session.setAttribute("selectDrugs",result.getData());
        return result;
    }
    //筛选tiaojian查询修改model返回原页面
    @RequestMapping("updateModel")
    public String updateModel(Model model,HttpSession session,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<Drug> drugslist  =(List<Drug>)session.getAttribute("selectDrugs");
        System.out.println("控制层修改model得到session："+drugslist);
        PageInfo<Drug> pageInfo = new PageInfo<Drug>(drugslist);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute(drugslist);
        return "drugStore/drugManage";
    }

    //药物管理页面
    @RequestMapping("toStorageManage")
    public String toStorageManage(HttpSession session,Model model){
        //查询是否session有药物数据

        //通过model把全部药物数据给页面
      /*  List<Drug> drugslist=drugsService.getAllDrugs(session,model);
        System.out.println("控制层查询全部药物数据："+drugslist);
        model.addAttribute(drugslist);*/
        return "drugStore/storageManage";
    }
    //药物更新操作
    @RequestMapping("updateDrugsInfo")
    @ResponseBody
    /*,@Param("productionDate") Date productionDate,@Param("qualityDate") Date qualityDate*/
    public ResponseResult updateDrugsInfo(Drug drug){
        System.out.println("控制层更新药品："+drug);
        ResponseResult result=drugsService.insertDrugs(drug);
        return result;
    }

    //随名字变化更新入库信息
    @RequestMapping("changStorageManage")
    @ResponseBody
    public ResponseResult changStorageManage(String name){
        //查询信息
        System.out.println("changStorageManage name:"+name);
        ResponseResult result=drugsService.findDrugsByName(name);
        return result;
    }

    //更新入库信息
    @RequestMapping("updateRuKu")
    @ResponseBody
    public ResponseResult updateRuKu(@Param("num") Integer num,@Param("num2") Integer num2,@Param("name") String name){
        num=num+num2;
        System.out.println(num);
        ResponseResult result=drugsService.updateKuCun(num,name);
        return result;
    }
}
